﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using OvenParameterForm.Model;
using OvenParameterForm.Util;
using Sunny.UI;
using System.IO;

namespace OvenParameterForm.Panel
{
    public partial class Frm_ovenSetting : UIForm
    {
        private string ovenSettingPath = Application.StartupPath + "\\系统配置\\";
        public Action<string> sendOvenName;
        public Frm_ovenSetting()
        {
            InitializeComponent();
        }

        private void tbn_saveAddress_Click(object sender, EventArgs e)
        {
            ovenPLCAddress oa=new ovenPLCAddress();
           PublicFunc.ControlToClass(this,tbx_充气地址, oa);
            if (tbx_烘箱名称.Text.Trim() == "") 
            {
                MessageBox.Show("烘箱名称不能为空","错误",MessageBoxButtons.OK,MessageBoxIcon.Information);
                return;
            }
                string path = ovenSettingPath + tbx_烘箱名称.Text.Trim() + ".xml";
            if (File.Exists(path))   //如果文档存在替换文档
            {
                XDocument xdoc = XDocument.Load(path);
                XElement xtemp = PublicFunc.ClassToXML(oa);
                xdoc.Element("Setting").ReplaceAll(xtemp);//这里右问题待处理
                xdoc.Save(path);
            }
            else
            {
                XDocument xdoc = new XDocument();
                xdoc.Add(new XElement("Setting"));//创建唯一的根节点
                xdoc.Root.Add(PublicFunc.ClassToXML(oa));
                xdoc.Save(path);
            }
            sendOvenName(tbx_烘箱名称.Text.Trim());
            this.Close();
        }
    }
}
